//
//  MarketRefreshViewController.m
//  bitcoin
//
//  Created by caiyajun on 2018/5/8.
//  Copyright © 2018年 比特币. All rights reserved.
//

#import "MarketRefreshViewController.h"
#import "MarketRefreshCell.h"
@interface MarketRefreshViewController ()

@end

@implementation MarketRefreshViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrowleft-icon"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    self.navigationItem.leftBarButtonItem = back;
}

- (void)backAction:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MarketRefreshCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MarketRefreshCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[MarketRefreshCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MarketRefreshCell"];
    }
    if (indexPath.row==0) {
        cell.marketRefreshLabel.text=@"始终实时行情";
    }else if (indexPath.row==1){
        
        cell.marketRefreshLabel.text=@"始终普通行情";
    }else{
        cell.marketRefreshLabel.text=@"仅在WiFi下实时行情";
    }
    cell.rightImage.hidden=YES;
    
    NSLog(@"refresh----->%@",[UserDefaults objectForKey:K_REFRESH]);
    if ([[UserDefaults objectForKey:K_REFRESH] isEqualToString:@"1"]&&[cell.marketRefreshLabel.text isEqualToString: @"始终实时行情"]) {
        cell.rightImage.hidden=NO;
    }else if ([[UserDefaults objectForKey:K_REFRESH] isEqualToString:@"2"]&&[cell.marketRefreshLabel.text isEqualToString: @"始终普通行情"]){
        cell.rightImage.hidden=NO;
    }else if ([[UserDefaults objectForKey:K_REFRESH] isEqualToString:@"3"]&&[cell.marketRefreshLabel.text isEqualToString: @"仅在WiFi下实时行情"]){
        cell.rightImage.hidden=NO;
    }
    
    
    return cell;
    
}

-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath

{
    MarketRefreshCell *cell = (MarketRefreshCell*)[tableView cellForRowAtIndexPath:indexPath];
    NSString*refresh;
    if ([cell.marketRefreshLabel.text isEqualToString: @"始终实时行情"]) {
        refresh=@"1";
    }else if ([cell.marketRefreshLabel.text isEqualToString: @"始终普通行情"]) {
        refresh=@"2";
    }else{
        refresh=@"3";
    }
    if ([UserDefaults objectForKey:K_LOGININFO_TOKEN]) {
        NSDictionary *params = @{@"refresh":refresh};
        [LSRequestManager putRequestWithURL:HUOBI(@"api/user/favSet")
                                     params:params
                                    success:^(NSDictionary *responseData) {
                                        if ([responseData[@"ret"] integerValue ] == 0) {
                                            //修改本地获取的语言文件-交替
                                            if ([cell.marketRefreshLabel.text isEqualToString: @"始终实时行情"]) {
                                                [UserDefaults setObject:@"1" forKey:K_REFRESH];
                                            }else if ([cell.marketRefreshLabel.text isEqualToString: @"始终普通行情"]){
                                                [UserDefaults setObject:@"2" forKey:K_REFRESH];
                                            }else if ([cell.marketRefreshLabel.text isEqualToString: @"仅在WiFi下实时行情"]){
                                                [UserDefaults setObject:@"3" forKey:K_REFRESH];
                                            }
                                            
                                            [UserDefaults synchronize];
                                            
                                            [self.tableView reloadData];
                                        } else {
                                            [MBProgressHUD showError:responseData[@"msg"]];
                                        }
                                    }
                                    failure:^(NSError *error) {
                                        
                                    }
                                    showHUD:NO];
    }else{
        if ([cell.marketRefreshLabel.text isEqualToString: @"始终实时行情"]) {
            [UserDefaults setObject:@"1" forKey:K_REFRESH];
        }else if ([cell.marketRefreshLabel.text isEqualToString: @"始终普通行情"]){
            [UserDefaults setObject:@"2" forKey:K_REFRESH];
        }else if ([cell.marketRefreshLabel.text isEqualToString: @"仅在WiFi下实时行情"]){
            [UserDefaults setObject:@"3" forKey:K_REFRESH];
        }
        [UserDefaults synchronize];
        
        [self.tableView reloadData];
    }
    
    
    
    
    
    
}











//-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
//
//{
//
//    MarketRefreshCell *cell = (MarketRefreshCell*)[tableView cellForRowAtIndexPath:indexPath];
//    cell.rightImage.hidden=NO;
//
//}
//- (void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath
//
//{
//
//    MarketRefreshCell *cell = (MarketRefreshCell*)[tableView cellForRowAtIndexPath:indexPath];
//    cell.rightImage.hidden=YES;
//
//}

@end
